import 'package:cached_network_image/cached_network_image.dart';
// import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/utils/utils_dlog.dart';

class NetworkLoadImage {
  static Widget loadNetworkImage(
    String? url, {
    double? width,
    double? height,
    BoxFit fit = BoxFit.cover,
    bool settingMax = true,
        Color? color,
  }) {
    // _checkMemory();
    // if (isCache == true) {
    // return CachedNetworkImage(
    //   imageUrl: url ?? '',
    //   width: width,
    //   height: height,

    //   fit: fit,
    //   memCacheWidth: settingMax == true ? 400 : null,
    //   // memCacheHeight: 150,
    //   // maxWidthDiskCache: settingMax == true ? (375.w).toInt() * 2 : null,
    //   // maxHeightDiskCache: settingMax == true ? (375.w).toInt() * 3 : null,
    //   // cacheManager: BaseCacheManager,
    //   // memCacheWidth: settingMax == true ? (375.w).toInt() : null,
    //   // memCacheHeight: settingMax == true ? (375.w).toInt() : null,
    //   // cacheManager: CacheManagerLogLevel.warning,
    // );
    // } else {
    // return ExtendedImage.network(
    //   url ?? '',
    //   height: height,
    //   width: width,
    //   fit: fit,
    //   // compressionRatio: 0.6,
    //   cache: false,
    //   loadStateChanged: ((ExtendedImageState state) {
    //     switch (state.extendedImageLoadState) {
    //       case LoadState.completed:
    //         return state.completedWidget;
    //       default:
    //         return SizedBox();
    //     }
    //     // return SizedBox();
    //   }),
    // );
    // }
    return CachedNetworkImage(
      imageUrl: url ?? '',
      width: width,
      height: height,
      fit: fit,
      memCacheHeight: settingMax == true ? 400 : null,
      color: color,
      // memCacheWidth: settingMax == true ? 400 : null,
      // maxWidthDiskCache: settingMax == true ? (375.w).toInt() * 2 : null,
      // maxHeightDiskCache: settingMax == true ? (375.w).toInt() * 3 : null,
    );
    // return Container(
    //   color: Colors.red,
    //   width: width,
    //   height: height,
    // );
    // }

    // return CachedNetworkImage(
    //   imageUrl: url ?? '',
    //   width: width,
    //   height: height,
    //   fit: fit,
    //   memCacheWidth: 200,
    //   memCacheHeight: 150,
    //   // maxWidthDiskCache: settingMax == true ? (375.w).toInt() * 2 : null,
    //   // maxHeightDiskCache: settingMax == true ? (375.w).toInt() * 3 : null,
    //   // cacheManager: BaseCacheManager,
    //   // memCacheWidth: settingMax == true ? (375.w).toInt() : null,
    //   // memCacheHeight: settingMax == true ? (375.w).toInt() : null,
    //   // cacheManager: CacheManagerLogLevel.warning,
    // );

    // return ExtendedImage.network(
    //   url ?? '',
    //   width: width,
    //   height: height,
    //   fit: fit,
    //   compressionRatio: compressionRatio,
    //   // cacheWidth: 100,
    //   maxBytes: 1024 * 1024,
    //   // cacheHeight: int.tryParse((height ?? 375.w).toString()),
    //   // cacheWidth: int.tryParse((width ?? 375.w).toString()),
    //   clearMemoryCacheWhenDispose: true,
    //   // cacheRawData: false,
    //   // handleLoadingProgress: false,
    //   // handleLoadingProgress: true,
    //   loadStateChanged: ((ExtendedImageState state) {
    //     switch (state.extendedImageLoadState) {
    //       case LoadState.completed:
    //         return state.completedWidget;
    //       default:
    //         return SizedBox();
    //     }
    //     // return SizedBox();
    //   }),
    // );
  }

  // static Widget loadNetworkBigImage(String? url,
  //     {double? width, double? height, BoxFit fit = BoxFit.cover}) {
  //   return ExtendedImage.network(
  //     url ?? '',
  //     width: width,
  //     height: height,
  //     fit: fit,
  //     compressionRatio: 0.2,
  //     clearMemoryCacheWhenDispose: true,
  //     loadStateChanged: ((ExtendedImageState state) {
  //       switch (state.extendedImageLoadState) {
  //         case LoadState.completed:
  //           return state.completedWidget;
  //         default:
  //           return SizedBox();
  //       }
  //       // return SizedBox();
  //     }),
  //   );
  // }

  static DecorationImage loadNetworkProviderImage(String? url,
      {BoxFit fit = BoxFit.cover}) {
    return DecorationImage(
        image: CachedNetworkImageProvider(url ?? '', maxWidth: (375.w).toInt()),
        //  ExtendedNetworkImageProvider(url ?? '',
        //     cacheRawData: false, scale: 0.1),
        fit: fit);
  }

  static _checkMemory() {
    ImageCache _imageCache = PaintingBinding.instance.imageCache;
    DLog(
        '_imageCache.currentSize   ${_imageCache.currentSize}   currentSizeBytes ${_imageCache.currentSizeBytes / 1024 / 1024}');
    if (_imageCache.currentSizeBytes >= 55 << 20) {
      _imageCache.clear();
      _imageCache.clearLiveImages();
    }
  }
}
